package com.nari.ies.ne.nw.server.service.fileQuality.impl;

import com.nari.ies.ne.nw.server.entity.FileQualityEntity;
import com.nari.ies.ne.nw.server.service.fileQuality.ISYZXXFileQualityService;
import com.nari.ies.ne.nw.server.util.DateUtil;
import com.nari.ies.ne.nw.server.util.IoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.Date;
@Slf4j
@Service
public class SYZXXFileQualityServiceImpl implements ISYZXXFileQualityService {

    @Override
    public FileQualityEntity FdSYZXXFileQuality(File file, FileQualityEntity fileQualityEntity) {
        // 文件是否延时
        int fileDelay = 0;
        // 文件内容是否为空
        int empty = 0;
        // 文件标签是否缺失
        int missingTag = 0;
        // 文件内容是否缺失
        int defect = 0;
        //文件格式是否有误（数据列分割后数目不对）
        int fileFormatError = 0;
        //文件内容格式是否有误（数据列类型转换报错）
        int fileContentError = 0;

        // 信息
        String errorInfo = fileQualityEntity.getMessage();
        //标题长度
        int titleCount = 0;
        //标签计数
        int count = 0;
        //数据类型 1：并网点，2：集电线
        int dataType = 0;
        //并网点内容缺失
        int bwdDefect = 0;
        //集电线内容缺失
        int jdxDefect = 0;

        //判断文件是否延时
        Date date = new Date(file.lastModified());
        fileDelay = DateUtil.getBetweenMinute(date, fileQualityEntity.getForecastDate());
        if (fileDelay == 0) {
            errorInfo = "文件延时";
        }

        // 判断内容是否为空
        if (file.length() == 0) {
            errorInfo = "文件内容为空";

            fileQualityEntity.setIsFileDelay(fileDelay);
            fileQualityEntity.setIsFileEmpty(empty);
            fileQualityEntity.setIsFileDefect(defect);
            fileQualityEntity.setIsFileMissTag(missingTag);
            fileQualityEntity.setIsFileFormatError(fileFormatError);
            fileQualityEntity.setIsFileContentError(fileContentError);
            fileQualityEntity.setMessage(errorInfo);

            return fileQualityEntity;
        } else {
            empty = 1;
            // 判断文件标签是否缺失
            FileReader fr = null;
            BufferedReader br = null;
            try {
                fr = new FileReader(file);
                br = new BufferedReader(fr);
                String len = "";

                // 标签字符拼接
                String plantCode = fileQualityEntity.getPlantCode();
                //主升压变
                String ZSYBFirstLabel = "<" + "ZSYB" + "::" + plantCode;
                String ZSYBEndLabel = "</" + "ZSYB" + "::" + plantCode + ">";
                //启备变
                String QBBFirstLabel = "<" + "QBB" + "::" + plantCode;
                String QBBEndLabel = "</" + "QBB" + "::" + plantCode + ">";
                //厂用变
                String CYBFirstLabel = "<" + "CYB" + "::" + plantCode;
                String CYBEndLabel = "</" + "CYB" + "::" + plantCode + ">";
                //联络变
                String LLBFirstLabel = "<" + "LLB" + "::" + plantCode;
                String LLBEndLabel = "</" + "LLB" + "::" + plantCode + ">";
                //并网点
                String BWDFirstLabel = "<" + "BWD" + "::" + plantCode;
                String BWDEndLabel = "</" + "BWD" + "::" + plantCode + ">";
                //升压站母线
                String SYZMXFirstLabel = "<" + "SYZMX" + "::" + plantCode;
                String SYZMXEndLabel = "</" + "SYZMX" + "::" + plantCode + ">";
                //无功补偿
                String WGBCFirstLabel = "<" + "WGBC" + "::" + plantCode;
                String WGBCEndLabel = "</" + "WGBC" + "::" + plantCode + ">";
                //集电线
                String JDXFirstLabel = "<" + "JDX" + "::" + plantCode;
                String JDXEndLabel = "</" + "JDX" + "::" + plantCode + ">";
                //断路器
                String DLQFirstLabel = "<" + "DLQ" + "::" + plantCode;
                String DLQEndLabel = "</" + "DLQ" + "::" + plantCode + ">";
                //刀闸
                String DZFirstLabel = "<" + "DZ" + "::" + plantCode;
                String DZEndLabel = "</" + "DZ" + "::" + plantCode + ">";
                //接地刀闸
                String JDDZFirstLabel = "<" + "JDDZ" + "::" + plantCode;
                String JDDZEndLabel = "</" + "JDDZ" + "::" + plantCode + ">";
                //等效发电机
                String DXFDJFirstLabel = "<" + "DXFDJ" + "::" + plantCode;
                String DXFDJEndLabel = "</" + "DXFDJ" + "::" + plantCode + ">";


                while ((len = br.readLine()) != null) {
                    if (len.length() == 0) {
                        continue;
                    }
                    // 分割字符串按照tab标签
                    String[] strs = len.split("\t");

                    if (len.contains("<")) {

                        //主升压变
                        if ((strs[0].equals(ZSYBFirstLabel) || strs[0].equals(ZSYBEndLabel))) {
                            count++;
                        }

                        //启备变
                        if ((strs[0].equals(QBBFirstLabel) || strs[0].equals(QBBEndLabel))) {
                            count++;
                        }
                        //厂用变
                        if ((strs[0].equals(CYBFirstLabel) || strs[0].equals(CYBEndLabel))) {
                            count++;
                        }
                        //联络变
                        if ((strs[0].equals(LLBFirstLabel) || strs[0].equals(LLBEndLabel))) {
                            count++;
                        }
                        //并网点开始标签
                        if ((strs[0].equals(BWDFirstLabel))) {
                            count++;
                            dataType = 1;
                        }
                        //并网点结束标签
                        if ((strs[0].equals(BWDEndLabel))) {
                            count++;
                            dataType = 0;
                        }
                        //升压站母线
                        if ((strs[0].equals(SYZMXFirstLabel) || strs[0].equals(SYZMXEndLabel))) {
                            count++;
                        }
                        //无功补偿
                        if ((strs[0].equals(WGBCFirstLabel) || strs[0].equals(WGBCEndLabel))) {
                            count++;
                        }
                        //集电线开始标签
                        if ((strs[0].equals(JDXFirstLabel) )) {
                            count++;
                            dataType = 2;
                        }
                        //集电线结束标签
                        if ((strs[0].equals(JDXEndLabel))) {
                            count++;
                            dataType = 0;
                        }

                        //断路器
                        if ((strs[0].equals(DLQFirstLabel) || strs[0].equals(DLQEndLabel))) {
                            count++;
                        }
                        //刀闸
                        if ((strs[0].equals(DZFirstLabel) || strs[0].equals(DZEndLabel))) {
                            count++;
                        }
                        //接地刀闸
                        if ((strs[0].equals(JDDZFirstLabel) || strs[0].equals(JDDZEndLabel))) {
                            count++;
                        }
                        //等效发电机
                        if ((strs[0].equals(DXFDJFirstLabel) || strs[0].equals(DXFDJEndLabel))) {
                            count++;
                        }

                    }

                    //并网点
                    if (dataType == 1) {
                        if (len.contains("@")) {
                            titleCount = strs.length;
                        }
                        if (len.contains("#")) {
                            bwdDefect = 1;
                            if (titleCount != strs.length) {
                                errorInfo = "文件格式错误";
                            } else {
                                for (int i = 4; i < strs.length; i++) {
                                    String shuntActive = strs[i];
                                    if (!shuntActive.equals("NULL") && !shuntActive.equals("null") && !shuntActive.equals(" ")) {
                                        Float.parseFloat(shuntActive);
                                    }
                                }
                            }
                        }
                    }

                    //集电线
                    if (dataType == 2) {
                        if (len.contains("@")) {
                            titleCount = strs.length;
                        }
                        if (len.contains("#")) {
                            jdxDefect = 1;
                            if (titleCount != strs.length) {
                                errorInfo = "文件格式错误";
                            } else {
                                fileFormatError = 1;

                                for (int i = 4; i < strs.length; i++) {
                                    String shuntActive = strs[i];
                                    if (!shuntActive.equals("NULL") && !shuntActive.equals("null") && !shuntActive.equals(" ")) {
                                        Float.parseFloat(shuntActive);
                                    }
                                }
                                fileContentError = 1;
                            }
                        }
                    }
                }

                if (count != 24) {
                    errorInfo = "文件标签缺失";
                }

                if (bwdDefect == 0) {
                    if(!errorInfo.equals("文件标签缺失".trim())){
                        errorInfo = "文件并网点内容缺失";
                    }
                }else if(jdxDefect == 0){
                    if(!errorInfo.equals("文件标签缺失".trim())){
                        errorInfo = "文件集电线内容缺失";
                    }
                }else {
                    defect = 1;
                }

            } catch (IOException e) {
                e.printStackTrace();

            } catch (NumberFormatException e) {
                errorInfo = "文件内容格式错误";
                e.printStackTrace();
            } finally {
                IoUtil.close(fr);
                IoUtil.close(br);

                fileQualityEntity.setIsFileDelay(fileDelay);
                fileQualityEntity.setIsFileEmpty(empty);
                fileQualityEntity.setIsFileDefect(defect);
                fileQualityEntity.setIsFileMissTag(missingTag);
                fileQualityEntity.setIsFileFormatError(fileFormatError);
                fileQualityEntity.setIsFileContentError(fileContentError);
                fileQualityEntity.setMessage(errorInfo);

                return fileQualityEntity;
            }
        }
    }


    @Override
    public FileQualityEntity GfSYZXXFileQuality(File file, FileQualityEntity fileQualityEntity) {
        // 文件是否延时
        int fileDelay = 0;
        // 文件内容是否为空
        int empty = 0;
        // 文件标签是否缺失
        int missingTag = 0;
        // 文件内容是否缺失
        int defect = 0;
        //文件格式是否有误（数据列分割后数目不对）
        int fileFormatError = 0;
        //文件内容格式是否有误（数据列类型转换报错）
        int fileContentError = 0;

        // 信息
        String errorInfo = fileQualityEntity.getMessage();
        //标题长度
        int titleCount = 0;
        //标签计数
        int count = 0;
        //数据类型 1：并网点，2：集电线
        int dataType = 0;
        //并网点内容缺失
        int bwdDefect = 0;
        //集电线内容缺失
        int jdxDefect = 0;

        //判断文件是否延时
        Date date = new Date(file.lastModified());
        fileDelay = DateUtil.getBetweenMinute(date, fileQualityEntity.getForecastDate());
        if (fileDelay == 0) {
            errorInfo = "文件延时";
        }

        // 判断内容是否为空
        if (file.length() == 0) {
            errorInfo = "文件内容为空";

            fileQualityEntity.setIsFileDelay(fileDelay);
            fileQualityEntity.setIsFileEmpty(empty);
            fileQualityEntity.setIsFileDefect(defect);
            fileQualityEntity.setIsFileMissTag(missingTag);
            fileQualityEntity.setIsFileFormatError(fileFormatError);
            fileQualityEntity.setIsFileContentError(fileContentError);
            fileQualityEntity.setMessage(errorInfo);

            return fileQualityEntity;
        } else {
            empty = 1;
            // 判断文件标签是否缺失
            FileReader fr = null;
            BufferedReader br = null;
            try {
                fr = new FileReader(file);
                br = new BufferedReader(fr);
                String len = "";

                // 标签字符拼接
                String plantCode = fileQualityEntity.getPlantCode();
                //并网点
                String BWDFirstLabel = "<" + "BWD" + "::" + plantCode;
                String BWDEndLabel = "</" + "BWD" + "::" + plantCode + ">";
                //主升压变
                String ZSYBFirstLabel = "<" + "ZSYB" + "::" + plantCode;
                String ZSYBEndLabel = "</" + "ZSYB" + "::" + plantCode + ">";
                //厂用变
                String CYBFirstLabel = "<" + "CYB" + "::" + plantCode;
                String CYBEndLabel = "</" + "CYB" + "::" + plantCode + ">";
                //集电线
                String JDXFirstLabel = "<" + "JDX" + "::" + plantCode;
                String JDXEndLabel = "</" + "JDX" + "::" + plantCode + ">";
                //升压站母线
                String SYZMXFirstLabel = "<" + "SYZMX" + "::" + plantCode;
                String SYZMXEndLabel = "</" + "SYZMX" + "::" + plantCode + ">";
                //无功补偿
                String WGBCFirstLabel = "<" + "WGBC" + "::" + plantCode;
                String WGBCEndLabel = "</" + "WGBC" + "::" + plantCode + ">";
                //断路器
                String DLQFirstLabel = "<" + "DLQ" + "::" + plantCode;
                String DLQEndLabel = "</" + "DLQ" + "::" + plantCode + ">";
                //刀闸
                String DZFirstLabel = "<" + "DZ" + "::" + plantCode;
                String DZEndLabel = "</" + "DZ" + "::" + plantCode + ">";
                //接地刀闸
                String JDDZFirstLabel = "<" + "JDDZ" + "::" + plantCode;
                String JDDZEndLabel = "</" + "JDDZ" + "::" + plantCode + ">";
                //等效发电机
                String DXFDJFirstLabel = "<" + "DXFDJ" + "::" + plantCode;
                String DXFDJEndLabel = "</" + "DXFDJ" + "::" + plantCode + ">";

                while ((len = br.readLine()) != null) {
                    if (len.length() == 0) {
                        continue;
                    }
                    // 分割字符串按照tab标签
                    String[] strs = len.split("\t");

                    if (len.contains("<")) {

                        //并网点开始标签
                        if ((strs[0].equals(BWDFirstLabel))) {
                            count++;
                            dataType = 1;
                        }
                        //并网点结束标签
                        if ((strs[0].equals(BWDEndLabel))) {
                            count++;
                            dataType = 0;
                        }

                        //主升压变
                        if ((strs[0].equals(ZSYBFirstLabel) || strs[0].equals(ZSYBEndLabel))) {
                            count++;
                        }
                        //厂用变
                        if ((strs[0].equals(CYBFirstLabel) || strs[0].equals(CYBEndLabel))) {
                            count++;
                        }
                        //集电线开始标签
                        if ((strs[0].equals(JDXFirstLabel) )) {
                            count++;
                            dataType = 2;
                        }
                        //集电线结束标签
                        if ((strs[0].equals(JDXEndLabel))) {
                            count++;
                            dataType = 0;
                        }
                        //升压站母线
                        if ((strs[0].equals(SYZMXFirstLabel) || strs[0].equals(SYZMXEndLabel))) {
                            count++;
                        }
                        //无功补偿
                        if ((strs[0].equals(WGBCFirstLabel) || strs[0].equals(WGBCEndLabel))) {
                            count++;
                        }
                        //断路器
                        if ((strs[0].equals(DLQFirstLabel) || strs[0].equals(DLQEndLabel))) {
                            count++;
                        }
                        //刀闸
                        if ((strs[0].equals(DZFirstLabel) || strs[0].equals(DZEndLabel))) {
                            count++;
                        }
                        //接地刀闸
                        if ((strs[0].equals(JDDZFirstLabel) || strs[0].equals(JDDZEndLabel))) {
                            count++;
                        }
                        //等效发电机
                        if ((strs[0].equals(DXFDJFirstLabel) || strs[0].equals(DXFDJEndLabel))) {
                            count++;
                        }

                    }

                    //并网点内容判断
                    if (dataType == 1) {
                        if (len.contains("@")) {
                            titleCount = strs.length;
                        }
                        if (len.contains("#")) {
                            bwdDefect = 1;
                            if (titleCount != strs.length) {
                                errorInfo = "文件格式错误";
                            } else {
                                for (int i = 4; i < strs.length; i++) {
                                    String shuntActive = strs[i];
                                    if (!shuntActive.equals("NULL") && !shuntActive.equals("null") && !shuntActive.equals(" ")) {
                                        Float.parseFloat(shuntActive);
                                    }
                                }
                            }
                        }
                    }
                    //集电线
                    if (dataType == 2) {
                        if (len.contains("@")) {
                            titleCount = strs.length;
                        }
                        if (len.contains("#")) {
                            jdxDefect = 1;
                            if (titleCount != strs.length) {
                                errorInfo = "文件格式错误";
                            } else {
                                fileFormatError = 1;

                                for (int i = 4; i < strs.length; i++) {
                                    String shuntActive = strs[i];
                                    if (!shuntActive.equals("NULL") && !shuntActive.equals("null") && !shuntActive.equals(" ")) {
                                        Float.parseFloat(shuntActive);
                                    }
                                }
                                fileContentError = 1;
                            }
                        }
                    }
                }

                if (count != 20) {
                    errorInfo = "文件标签缺失";
                }

                if (bwdDefect == 0) {
                    if(!errorInfo.equals("文件标签缺失".trim())){
                        errorInfo = "文件并网点内容缺失";
                    }
                }else if(jdxDefect == 0){
                    if(!errorInfo.equals("文件标签缺失".trim())){
                        errorInfo = "文件集电线内容缺失";
                    }
                }else {
                    defect = 1;
                }

            } catch (IOException e) {
                e.printStackTrace();

            } catch (NumberFormatException e) {
                errorInfo = "文件内容格式错误";
                e.printStackTrace();
            } finally {
                IoUtil.close(fr);
                IoUtil.close(br);

                fileQualityEntity.setIsFileDelay(fileDelay);
                fileQualityEntity.setIsFileEmpty(empty);
                fileQualityEntity.setIsFileDefect(defect);
                fileQualityEntity.setIsFileMissTag(missingTag);
                fileQualityEntity.setIsFileFormatError(fileFormatError);
                fileQualityEntity.setIsFileContentError(fileContentError);
                fileQualityEntity.setMessage(errorInfo);

                return fileQualityEntity;
            }
        }
    }
}
